<template>
	<view>
		<view>
			<tui-tag type="black" plain="15%">新增设备</tui-tag>
			<br>
			<input type="text" v-model="email" placeholder="设备Sn码" />
			<br>
			<input  type="text"  v-model="pwd" placeholder="设备Onboarding Key" />
			<br>
			<tui-button type="blue" shape="circle"@click="add">确认</tui-button>
		</view>
	</view>

</template>

<script>
	import tui from '../../common/httpRequest'
	import JSEncrypt from '../../common/jsencrypt/bin/jsencrypt.js'
	export default {
		data() {
			return {
				email:'',
				pwd: ''
			}
		},
		methods: {
					
			add(){
				console.log(this.$data.email);
				console.log(this.$data.pwd)
				
				var uname = this.$data.email;
				var pass = this.$data.pwd;
				console.log(uname);
				console.log(pass)
					
				var body = {
				"sn":uname,
				"key":pass,
				"response": ""
				}
				
				uni.request({
				    url: 'https://panel.piscesiot.com/api/consumer/AddHotspot?sn='+uname+"&key="+pass, //仅为示例，并非真实接口地址。
				    header: {
				        'Accept': 'application/json' ,
						'Authorization': 'Bearer '+getApp().globalData.token
				    },
				    success: (res) => {
						
						
						if(res.data.code!=200){
							uni.showToast({
								title:"网络错误，请检查"
							})
							
						}else{
							this.ReadAnalyze();
							this.ReadDevice();
						}
				    }
				});
				
				//this.req("https://panel.piscesiot.com/api/consumer/AddHotspot",body,1)
			},
				
			encode(data){
				const encryptor = new JSEncrypt;
				const publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYm/KYS2fkjLlOyD1holEC5EbM1ZWLr1yHqxZ1KLFA SYC/wPKOVILXXDhS8PKuaI+cDLcRTDO02G38qqUSqG9FZ580E+rir1NHYa+zPAVJa/REzBHVY9TwAjWe V1gkxvo/1ystkEei7AwswOVgcGTniLQ8HyLQ2ieo0ZpBBYzRFwIDAQAB"
				encryptor.setPublicKey(publicKey)
				// 这个就是rsa加密后的值
				var ret = encryptor.encrypt(data)
				return ret;
			},
			req(url,data,type){
			let postData = {}
			tui.request(url, "GET", data, false,true, type == 1 ? false : true).then((res) => {
					
				if(res.code==200){
					this.loginSuccess(res);
				}else{
					uni.showToast({
						title:"SN码与key不匹配"
					})
				}
				
			}).catch((res) => {
				this.result = `${type == 1 ? "带" : "无"}loading请求失败！ ${JSON.stringify(res)}`
			})	
			},
			loginSuccess(res){
				console.log(res.data);
				getApp().globalData.token = res.data.token;
				this.ReadAnalyze();
				this.ReadDevice();
			},
				
			ReadAnalyze(){
				uni.request({
				    url: 'https://panel.piscesiot.com/api/consumer/UserAnalyze', //仅为示例，并非真实接口地址。
				    header: {
				        'Accept': 'application/json' ,
						'Authorization': 'Bearer '+getApp().globalData.token
				    },
				    success: (res) => {
						
						
						if(res.data.code!=200){
							uni.showToast({
								title:"网络错误，请检查"
							})
							
						}else{
							getApp().globalData.analyz = res.data.data
							console.log(getApp().globalData.analyz);
						}
				    }
				});
			},
			ReadDevice(){
				uni.request({
				    url: 'https://panel.piscesiot.com/api/consumer/UserReadDeviceList', //仅为示例，并非真实接口地址。
				    header: {
				        'Accept': 'application/json' ,
						'Authorization': 'Bearer '+getApp().globalData.token
				    },
				    success: (res) => {
						
						
						if(res.data.code!=200){
							uni.showToast({
								title:"网络错误，请检查"
							})
							
						}else{
							getApp().globalData.device = res.data.data.device
							console.log(getApp().globalData.device);
							uni.navigateTo({
								"url":"../dashboard/dashboard"
							})
						}
				    }
				});
			},
			RamColor(){
					var ram = Math.floor(Math.random() * 3 + 1);
						
					switch (ram){
					case 1:
						return "#669999"; //grey
					case 2:
						return "#ff5739";//red
					default:
						return "#9bbeff";//blue
					}
				},
		
		}
	}
</script>

<style>
.uni-picker-tips {
	font-size: 12px;
	color: #666;
	margin-bottom: 15px;
	padding: 0 15px;
	/* text-align: right; */
}
</style>
